﻿/*
--------------------------------------
File: BlobDetection.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
A algorithm that creates and filters blobs for the licenseplate
Uses the functions in BlobUtils.cs, tries to filter small blobs, blobs that has the wrong dimensions and returns the biggest blob if multiple blobs are
still left after filtering
--------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace THO7_Team8
{
	internal class LicensePlateBlobDetection : VisionAlgorithm
	{
		public LicensePlateBlobDetection(String name)
			: base(name)
		{
			hasConfiguration = false;
		}

		public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
		{
			Bitmap returnBitmap = new Bitmap(sourceImage);
			int blobColor = WHITE;
			int[] array = MyImage.BmpToArray(returnBitmap);
			int width = returnBitmap.Width;

			//Resets all the static class variables used in BlobUtils
			BlobUtils.initialize();

			//Find all the blobs in the image
			BlobUtils.findBlobs(ref array, blobColor, width);

			//Filters blobs that are smaller than 0,1% of the total size
			BlobUtils.filterSmallBlobs(array.Length * 0.001);
			//If all blobs are filtered because they are to small, try again with a smaller minsize
			if(BlobUtils.getGoodBlobListSize() < 1)
			{
				BlobUtils.filterSmallBlobs(array.Length * 0.0001);
			}

			//Filter blobs that do not have the correct dimensions
			BlobUtils.filterDimensions(width, ref array);

			//Filters all but the biggest blob if there are multiple blobs on the goodBlobList
			BlobUtils.multipleBlobFilter();
			//Filters all the blobs that are not on the goodBlobList
			BlobUtils.filterBlobs(ref array);

			returnBitmap = MyImage.ArrayToBmp(array, returnBitmap);
			return returnBitmap;
		}
	}
}